<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true"
          >添加分类</el-button
        >
      </div>
      <el-table :data="list" style="width: 100%">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="分类名称" prop="cate_name"> </el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button size="mini" type="primary" @click="showEditHandler(scope.row)" >编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteArtHandler(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="添加文章分类" :visible.sync="addVisible" width="35%" @closed="$refs.addForm.resetFields()">
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateHandler">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改文章分类" :visible.sync="editVisible" width="35%" @closed="$refs.editForm.resetFields()">
      <el-form
        :model="editForm"
        :rules="addRules"
        ref="editForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editForm = false">取 消</el-button>
        <el-button type="primary" @click="editCateHandler">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      list: [],
      addVisible: false,
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '必须是1-10位字母数字', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^\S{1,15}$/, message: '必须是1-15位字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$axios.get('/my/cate/list')
      // console.log(res)
      if (res.code === 0) {
        this.list = res.data
      }
    },
    addCateHandler () {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success('添加文章分类成功')
        this.getCateList()
      })
      this.addVisible = false
    },
    showEditHandler (row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许修改')
      this.editForm = { ...row }
      this.editVisible = true
    },
    editCateHandler () {
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success('添加文章分类成功')
        this.getCateList()
      })
      this.editVisible = false
    },
    async deleteArtHandler (id) {
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除')
      const { data: res } = await this.$axios.delete('/my/cate/del', { params: { id } })
      console.log(res)
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.error(res.message)
      this.getCateList()
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
